package edu.ccut.saturn.studio.database.ui;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * 功能：数据库类型及驱动器手动选择
 * 
 * @author Tony
 * @author 谷钰
 * 
 * @version 1.0 2008.12.04
 */
public class DriverClassHelpers {

	private static final String DB2 = "DB2";

	private static final String SQLSERVER = "SQLServer";

	private static final String ORACLE = "Oracle";
	
	private static final String MYSQL = "MySQL";
	

	private static final String DB2_DRIVER = "com.ibm.db2.jcc.DB2Driver";
	
	//private static final String SQLSERVER_DRIVE = "net.sourceforge.jtds.jdbc.Driver";//修要修改
	private static final String SQLSERVER_DRIVE = "com.microsoft.sqlserver.jdbc.SQLServerDriver";

	private static final String ORACLE_DRIVER = "oracle.jdbc.driver.OracleDriver";
	
	private static final String MYSQL_DRIVER = "com.mysql.jdbc.Driver";
	

	private static final String DB2_DIALECT = "org.hibernate.dialect.DB2Dialect";

	private static final String SQLSERVER_DIALECT = "org.hibernate.dialect.SQLServerDialect";

	private static final String ORACLE_DIALECT = "org.hibernate.dialect.OracleDialect";
	
	private static final String MYSQL_DIALECT = "org.hibernate.dialect.MySQLDialect";

	private Map<String, String> dialects = new HashMap<String, String>();

	private Map<String, String> connectionUrls = new HashMap<String, String>();

	private Map<String, String> driverClasses = new HashMap<String, String>();

	public DriverClassHelpers() {
		dialects.put(ORACLE, ORACLE_DIALECT);
		dialects.put(SQLSERVER, SQLSERVER_DIALECT);
		dialects.put(DB2, DB2_DIALECT);
		dialects.put(MYSQL, MYSQL_DIALECT);

		driverClasses.put(ORACLE, ORACLE_DRIVER);
		
		driverClasses.put(MYSQL, MYSQL_DRIVER);

		driverClasses
				.put(SQLSERVER, SQLSERVER_DRIVE);

		driverClasses.put(DB2, DB2_DRIVER);

		connectionUrls.put(ORACLE,
				"jdbc:oracle:thin:@localhost:1521:orcl");
		connectionUrls.put(MYSQL,
				"jdbc:mysql://localhost:3306/database");
		connectionUrls.put(DB2, "jdbc:db2://localhost:50000/saturn");
		connectionUrls.put(SQLSERVER,
				"jdbc:sqlserver://localhost;DatabaseName=");
	}

	public Collection<String> getDialectNames() {
		return this.dialects.keySet();
	}

	/**
	 * 
	 * @param dialectName
	 * @return corresponding class name if available, otherwise return
	 *         dialectName assuming it is a "raw" classname
	 */
	public String getDialectClass(String dialectName) {
		if (this.dialects.containsKey(dialectName)) {
			return this.dialects.get(dialectName);
		}

		return dialectName;
	}

	public String getDriverClasses(String dialectName) {
		if (this.driverClasses.containsKey(dialectName)) {
			return this.driverClasses.get(dialectName);
		}
		
		return dialectName;
	}

	public String getConnectionURLS(String driverclass) {
		if (this.connectionUrls.containsKey(driverclass)) {
			return this.connectionUrls.get(driverclass);
		}
		
		return driverclass;
	}
}
